//
//  MJGHTMLReader.h
//  MJGHTMLReader
//
//  Created by Mei Jigao on 15/4/21.
//  Copyright (c) 2015年 MeiJigao. All rights reserved.
//

#import <Foundation/Foundation.h>

@class HTMLDocument;
@class HTMLElement;
@class HTMLNode;

@class MJGHTMLReader;
@protocol MJGHTMLReaderUrlDocumentDelegate <NSObject>

@required
- (void)htmlFieldRead:(MJGHTMLReader *)htmlReader didFinisheLoading:(NSError *)loadError;

@end

@interface MJGHTMLReader : NSObject

@property (nonatomic, strong, readonly) NSURL *documentUrl;
@property (nonatomic, strong, readonly) HTMLDocument *rootDocument;

- (instancetype)init NS_DESIGNATED_INITIALIZER;

#pragma mark - document
- (HTMLDocument *)setDocumentElementWithString:(NSString *)string;
- (HTMLDocument *)setDocumentElementWithData:(NSData *)htmlData;
- (void)setDocumentElementWithUrl:(NSURL *)htmlUrl delegate:(id<MJGHTMLReaderUrlDocumentDelegate>)delegate;

#pragma mark - element
/**
 * <html><head></head><body></body><html/>
 * 获取<html>内的第一层节点
 * 不包括嵌套节点
 */
- (HTMLElement *)htmlElement;

/**
 * <head></head>
 * 获取<head>内的第一层节点
 * 不包括嵌套节点
 */
- (HTMLElement *)headElement;

/**
 * <body></body>
 * 获取<body>内的第一层节点
 * 不包括嵌套节点
 */
- (HTMLElement *)bodyElement;

#pragma mark - attributes
/**
 * <xxx><xxx/>
 * 获取<xxx>内的第一层节点
 * 不包括嵌套节点
 */
- (NSArray *)elementNodesIn:(HTMLNode *)htmlNode;

/**
 * <xxx><tagName><tagName/><tagName><tagName/><xxx/>
 * 获取<xxx>内的第一层<aaa>节点
 * 不包括嵌套节点
 */
- (NSArray *)elementNodesIn:(HTMLNode *)htmlNode tagName:(NSString *)tagName;

/**
 * <meta http-equiv="X-UA-Compatible" tagName="IE=edge"></meta>
 * 获取<meta>内的各字段内容信息
 * 不包括嵌套节点
 * 数组元素节点格式
 * {
 *      content = "IE=edge";
 *      "http-equiv" = "X-UA-Compatible";
 * }
 */
- (NSArray *)attributesDictArrayIn:(HTMLNode *)htmlNode;

/**
 * <meta http-equiv="X-UA-Compatible" tagName="IE=edge"></meta>
 * 获取<meta>内的各字段内容信息
 * 不包括嵌套节点
 * 数组元素节点格式
 * {
 *      content = "IE=edge";
 *      "http-equiv" = "X-UA-Compatible";
 * }
 */
- (NSArray *)attributesDictArrayIn:(HTMLNode *)htmlNode tagName:(NSString *)tagName;

/**
 * 多层遍历，获取指定内容
 * eg. <head><meta content="古筝" name="app-share-title"><head>
 * 获取 content 的内容 古筝
 * 参数设置为
 * attributeName    ->  content
 * specifyName      ->  name
 * specifyAttribute ->  app-share-title
 */
- (NSString *)attributeIn:(HTMLNode *)htmlNode attributeName:(NSString *)attributeName specifyAttributeName:(NSString *)specifyName specifyAttribute:(NSString *)specifyAttribute;

#pragma mark - Log
- (void)logOutHTMLElement;

@end

